/*
 * GUICadastroCategorias.java
 *
 * Created on 15/05/2011, 16:07:32
 */

package gui;

import dao.DAOCategoria;
import javax.swing.JOptionPane;
import model.Categoria;
import model.Utils;

/**
 * Interface utilizada para cadastrar novas categorias e alterar categorias existentes
 * @author Janio Luiz Antunes
 * @author Marcel Vinicius Kohls
 * @see http://code.google.com/p/sysdesk-java/
 *
 */
public class GUICadastroCategorias extends javax.swing.JFrame {

    Categoria categoriaOrigem = new Categoria();
    DAOCategoria daoCategoria = new DAOCategoria();
    private Utils util = new Utils();

    /** 
     * inicializa o form de cadastro/alteracao de categorias GUICadastroCategorias
     */
    public GUICadastroCategorias() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel3 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jtfCatCodigo = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jtfCatDescricao = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jbCatSalvar = new javax.swing.JButton();
        jbCatCancelar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        jLabel3.setText("jLabel3");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("SysDesk - Cadastro de Categorias");
        setResizable(false);

        jtfCatCodigo.setEditable(false);

        jLabel1.setText("Código");

        jLabel2.setText("Descrição");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfCatCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfCatDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, 317, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCatCodigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfCatDescricao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbCatSalvar.setText("Salvar");
        jbCatSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCatSalvarActionPerformed(evt);
            }
        });

        jbCatCancelar.setText("Cancelar");
        jbCatCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCatCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(218, Short.MAX_VALUE)
                .addComponent(jbCatCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbCatSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbCatSalvar)
                    .addComponent(jbCatCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     *
     * Salva o cadastro da categoria no banco
     */
    private void jbCatSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCatSalvarActionPerformed
        try{
            categoriaOrigem.setDescricao(jtfCatDescricao.getText());

            if  (daoCategoria.salvar(categoriaOrigem) == true) {
                JOptionPane.showMessageDialog(null, "Categoria salva com sucesso!");
                this.dispose();
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Erro: "+e.getMessage());
        }

    }//GEN-LAST:event_jbCatSalvarActionPerformed

    /**
     *
     * Fecha esta janela
     */
    private void jbCatCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCatCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbCatCancelarActionPerformed
    
    /**
     * Define o código do registro a ser alterado/incluido.
     * @param pCodigoCategoria
     */
    public void setCodigoSelecionado(int pCodigoCategoria){
        if (pCodigoCategoria != 0) {
            try{
                System.out.println("setCodigoCategoria: Codigo="+Integer.toString(pCodigoCategoria));
                categoriaOrigem = daoCategoria.getCategoriaPorCodigo(pCodigoCategoria);
            } catch (Exception e) {
                System.out.println("erro: setCodigoSelecionado: "+e.getMessage());
            }
        }

        jtfCatCodigo.setText(Integer.toString(categoriaOrigem.getCodigo()));
        jtfCatDescricao.setText(categoriaOrigem.getDescricao());
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUICadastroCategorias().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbCatCancelar;
    private javax.swing.JButton jbCatSalvar;
    private javax.swing.JTextField jtfCatCodigo;
    private javax.swing.JTextField jtfCatDescricao;
    // End of variables declaration//GEN-END:variables

}
